﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Fitment
{
    public partial class ShowDelay : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                bindList();   //对控件进行绑定
                this.DataList1.ShowFooter = false;   //不显示页脚
            }
        }

        DS data = new DS();  //新建数据库连接
        protected void bindList()
        {
            int id = Convert.ToInt32(Request.QueryString["delay_id"]);  //获取延迟工程ID
            string strSql = "SELECT *From ProjectInfo WHERE Pro_ID='" + id + "'";
            DataTable table = data.GetDataSource(strSql, "Query");
            this.DataList1.DataSource = table.DefaultView;
            DataList1.DataBind();   //对数据进行

        }

        protected void submit_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(Request.QueryString["delay_id"]);  //获取延迟工程ID
            String strSql = "UPDATE ProjectInfo SET State='Y' WHERE Pro_ID='" + id + "'";   //表示已完成工程
            if (data.ExcuteSql(strSql))
            {
                bindList();   //如果更新成功则重新绑定控件
                Label state = (Label)this.DataList1.Items[0].FindControl("state");
                state.Text = "已完成";
            }
            else
                return;

        }

        protected void modify_Click(object sender, EventArgs e)
        {

            this.DataList1.ShowFooter = true;   //用于显示页脚控件

        }

        protected void submit_Click1(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(Request.QueryString["delay_id"]);
            TextBox Date = (TextBox)this.DataList1.Controls[this.DataList1.Controls.Count - 1].FindControl("proTime");
            DateTime strChange = Convert.ToDateTime(Date.Text.ToString());    //获取变更起始时间
            string strSql = "SELECT NeedTime FROM Material,ProjectInfo WHERE Pro_ID='" + id + "' AND ProjectName=ProName";
            DataTable table = data.GetDataSource(strSql, "Query");
            int td = Convert.ToInt32(table.Rows[0][0]);   //将需要的时间转换成时间间隔
            DateTime finish = strChange.AddDays(td);
            strSql = "UPDATE ProjectInfo SET StartDate='" + strChange + "', FinishDate='" + finish + "',State='N' WHERE Pro_ID='" + id + "'";  //更新数据库中的完成时间
            if (data.ExcuteSql(strSql))
            {
                bindList();   //重新绑定控件
                this.DataList1.ShowFooter = false;
            }
            else
                return;

        }

        protected void cancel_Click(object sender, EventArgs e)
        {
            this.DataList1.ShowFooter = false;   //隐藏页脚
            bindList();
        }
    }
    
}
